/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.me

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import kotlinx.android.synthetic.main.activity_edit_signature.*
import kotlinx.android.synthetic.main.include_header.*
import top.stime21.myneighbours.R
import top.stime21.myneighbours.api.ApiManager
import top.stime21.myneighbours.base.BuzBaseActivity
import top.stime21.myneighbours.base.io_main
import top.stime21.myneighbours.data.entity.User
import top.stime21.myneighbours.utils.ACache

class EditSignatureActivity : BuzBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_signature)
        initData()
    }

    private lateinit var user: User

    private fun initData() {
        tv_title.setText("编辑签名")
        btn_back.setOnClickListener { finish() }
        btn_right.visibility = View.VISIBLE
        user = ACache.get(this).getAsObject("user") as User
        et_signature.setText(user.signature)
        btn_right.setOnClickListener({
            var et_value: String = et_signature.getText().toString()
            if (TextUtils.isEmpty(et_value)) {
                showToast("昵称不能为空")
                return@setOnClickListener
            }
            showLoadding()
            ApiManager.ins().dataApi.updateInfo("signature", et_signature.text.toString(), user.userId).io_main().subscribe(
                    { result ->
                        if (result.isSuccess && result.data != null) {
                            ACache.get(this@EditSignatureActivity).put("user", result.data)
                            finish()
                        }
                        hideLodding()
                    }, { t ->
                hideLodding()
                showToast("请求失败")
            })
        })
    }
}
